package com.nbicc.news.service.impl;

import com.github.pagehelper.Page;
import com.nbicc.ad.po.AdInstance;
import com.nbicc.common.Const;
import com.nbicc.common.RequestPageObj;
import com.nbicc.common.ResponseObj;
import com.nbicc.news.mapper.NewsInfoMapper;
import com.nbicc.news.po.NewsInfo;
import com.nbicc.news.service.INewsInfoService;
import com.nbicc.news.vo.NewsPreviousNext;
import com.nbicc.parent.serivce.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.io.Serializable;
import java.util.*;

@Service("NewsInfoServiceImpl")
public class NewsInfoServiceImpl extends BaseService implements INewsInfoService {

    @Autowired
    private NewsInfoMapper newsInfoMapper;

    @Override
    public NewsInfo selectById(Number id) {
        return newsInfoMapper.selectById(id);
    }

    @Override
    public ResponseObj find(RequestPageObj<Map<String, Object>> requestPageObj) {
        setPageHelper(requestPageObj , NewsInfo.TABLE_NEWS_INFO);
        List<NewsInfo> newsInfos = newsInfoMapper.selectByMap(requestPageObj.getData());
        return new ResponseObj(Const.HTTP_STATUS_SUCCESS, newsInfos, ((Page<?>) newsInfos).getTotal());
    }

    @Override
    public List<NewsInfo> selectBatchIds(Set ids) {
        if(CollectionUtils.isEmpty(ids)){
            return Collections.EMPTY_LIST;
        }else{
            return newsInfoMapper.selectBatchIds(ids);
        }
    }

    @Override
    public NewsPreviousNext getPreviousAndNext(int catId, long newsId) {
        NewsInfo newsInfoPrevious = newsInfoMapper.selectPrevious(catId , newsId);
        NewsInfo newsInfoNext = newsInfoMapper.selectNext(catId , newsId);
        return NewsPreviousNext.builder().newsInfoPrevious(newsInfoPrevious).newsInfoNext(newsInfoNext).build();
    }

    @Override
    public Integer insert(NewsInfo newsInfo) {
        return newsInfoMapper.insert(newsInfo);
    }

    @Override
    public Integer updateById(Object newsInfo) {
        return newsInfoMapper.updateById(newsInfo);
    }

    @Override
    public Integer deleteById(Number id) {
        return newsInfoMapper.deleteById(id);
    }

}
